import { getSDK } from "./SDKDefine";

let URL = 'http://sarsgame.gitee.io/configs/treehouse/'
export class sly_sdk {
    private cfg: { channel: string, appId: string, secret: string, url?: string } = null;
    protected appId: string = ''
    protected secret: string = ''
    protected url: string = ''
    initSdkByConf(opt: { channel: string, appId: string, secret: string, url?: string }) {
        this.cfg = opt
        this.appId = opt.appId;
        this.secret = opt.secret;
        if (opt.url) {
            this.url = opt.url;
        } else {
            this.url = URL;
        }
    }

    init(openID: string) {

    }

    login(ok: (openid) => void, fail: (err) => void) {
        ok('')
    }

    loadAd(func: (res: any) => void) {
        func({})
    }

    loadCfg(ok: (result) => void, fail: (err) => void) {
        if (!this.cfg) {
            fail('not initSdkByConf ')
        }
        let url = this.url + this.cfg.channel + '.json';
        console.log(' url ', url)
        this.request(url, (err, data) => {
            console.log('err ', err, data)
            if (!err) {
                ok(data)
            } else {
                fail(' data is null ')
            }
        })

    }

    navigate2Mini(data: any, openID: string, success: Function, fail: Function, complete: Function) {

    }

    request(url: string, func: (msg: string, data: any) => void) {
        let sdk = getSDK();
        if (sdk) {
            sdk.request({
                url: url, //仅为示例，并非真实的接口地址
                timeout: 15000,
                header: {
                    'content-type': 'application/json', // 默认值
                    'If-Modified-Since': '0'
                },
                success(res) {
                    // console.log('request success', res.data)
                    func(null, res.data)
                },
                fail(err) {
                    // console.log('request err', err)
                    func(' error ', err)
                }
            })
        }

    }
}